﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Globalization;
using System.ComponentModel;
using System.Data.SqlClient;

namespace frmCheckStock
{
    public partial class frmCategory : Form
    {
        string categoryid;

        public frmCategory()
        {
            InitializeComponent();
        }

        private void frmCategory_Load(object sender, EventArgs e)
        {
            txtCategoryID.Text = AutoGenerateCode();
        }

        #region AutoGenCategoryID
        string AutoGenerateCode()
        {
            string newcategoryid = "";
            CategoryBroker cat = new CategoryBroker();
            newcategoryid = cat.AutoGenCategoryID();

            return newcategoryid;
        }
        #endregion


        #region Validation
        private Boolean MyValidation()
        {
            if (txtCategoryName.Text.Trim() == "")
            {
                errorProvider1.SetError(txtCategoryName, "Please Type Category Name");
                return false;
            }
            else
            {
                return true;
            }
        }
        #endregion

        private void btnSave_Click(object sender, EventArgs e)
        {
            Category info = new Category();
            CategoryBroker controller = new CategoryBroker();

            Boolean myvalidate;
            myvalidate = MyValidation();

            try
            {
                if (myvalidate == true)
                {
                    controller.Category_Insert(txtCategoryID.Text, txtCategoryName.Text);
                    MyMessageBox.ShowBox("Saved Successful!");
                    txtCategoryName.Text = "";
                    txtCategoryID.Text = AutoGenerateCode();

                }
            }
            catch (SqlException ex)
            {
                MyMessageBox.ShowBox("SQL Error " + ex.Number + ":" + ex.Message);
            }
        }

    }
}
